package com.woniuxy.qiantai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.qiantai.entity.Address;
import com.woniuxy.qiantai.mapper.AddressMapper;
import com.woniuxy.qiantai.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author woniumrwang
 * @since 2023-01-06 02:44:35
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    AddressMapper addressMapper;

    @Override
    public List<Address> getAll(Long userId) {

        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);

        List<Address> addressList = addressMapper.selectList(queryWrapper);

        return addressList;
    }

    @Override
    @Transactional
    public void addAddress(Long userId, Address address) {

        if (address.getIsDefault().equals("1")){
            //将原有的所有地址置为非默认地址
            addressMapper.clearIsDefault(userId);
        }

        //再写入新的地址
        address.setUserId(userId);
        address.setStatus("1");

        addressMapper.insert(address);

    }
}
